


*-------------------------------------------------------------------------------
* collect contact card, ISR, traffic, and beat population data 
*-------------------------------------------------------------------------------

	/* Note: We only use 2014–2016 data in this figure. */
	
* contact cards 
use "data/outputs/CC_beat.dta", clear
drop if month < tm(2014m1) | month > tm(2016m12)
keep beat month pedestrian 
rename pedestrian cc_count_pedestrian
drop if beat == "1651" | beat == "1652" | beat == "1653" | beat == "1654" | beat == "1655" /* O'Hare */
drop if beat == "4100" | beat == "5100" /* Non-geographic beats */
tempfile cc
save `cc'

* ISR
use "data/outputs/ISRpedestrian_beat.dta", clear
drop if month < tm(2014m1) | month > tm(2016m12)
keep beat month isr_count_pedestrian
drop if beat == "1651" | beat == "1652" | beat == "1653" | beat == "1654" | beat == "1655" /* O'Hare */
drop if beat == "4100" | beat == "5100" /* Non-geographic beats */
tempfile isr
save `isr'

* population
import delimited "data/inputs/other/beatpop.txt", delimiter(space) clear 
drop if _n == 1
rename v1 beat
rename v2 population
drop v3
tostring beat, replace
replace beat = "0" + beat if length(beat) == 3
tempfile beatpop
save `beatpop'

* traffic stops
use "data/outputs/TSSS_beat.dta", clear
drop if month < tm(2014m1) | month > tm(2016m12)
rename beatlocation beat
keep if agencyname == "CHICAGO POLICE"
keep beat month count
drop if beat == ""
drop if beat == "1651" | beat == "1652" | beat == "1653" | beat == "1654" | beat == "1655" /* O'Hare */
rename count itsscount
tempfile tsss
save `tsss'

			
*-------------------------------------------------------------------------------
* merge
*-------------------------------------------------------------------------------

* isr data
use `tsss', clear
merge 1:1 beat month using `isr' /* Note, ISR data starts in 2016; 
								    all _m == 1s are in 2014–2015.. */
drop _m


* cc data
merge 1:1 beat month using `cc' 
replace itsscount = 0 if _m == 2
replace isr_count_pedestrian = 0 if _m == 2
drop _m


*-------------------------------------------------------------------------------
* reshape, calculate rates and changes
*-------------------------------------------------------------------------------

* keep just six periods: first half of 2014, 2015, 2016
keep if (month >= tm(2014m1) & month <= tm(2014m6)) | ///
		(month >= tm(2015m1) & month <= tm(2015m6)) | ///
	    (month >= tm(2016m1) & month <= tm(2016m6)) 
gen period = 14 if yofd(dofm(month)) == 2014
replace period = 15 if yofd(dofm(month)) == 2015
replace period = 16 if yofd(dofm(month)) == 2016

* collapse to period-beat level
collapse (sum) itsscount isr_count_pedestrian cc_count_pedestrian, by(beat period)

* reshape
reshape wide itsscount isr_count_pedestrian cc_count_pedestrian, i(beat) j(period)

* these categories doesn't exist: no ISRs in 2015
drop isr_count_pedestrian14 isr_count_pedestrian15

* population
merge 1:1 beat using `beatpop'
drop if _m == 2
drop _m

* rates
foreach var of varlist itsscount* cc_count_pedestrian* isr_count_pedestrian16 {
	gen `var'_per100 = 100 * `var' / pop
	}

	
* changes
gen peddiff_15_14 = cc_count_pedestrian15_per100 - cc_count_pedestrian14_per100
gen peddiff_16_15 = isr_count_pedestrian16_per100 - cc_count_pedestrian15_per100
	/* Note: Results are nearly identical using cc_count_pedestrian16 instead of ISR count. */
gen trafficdiff_15_14 = itsscount15_per100 - itsscount14_per100
gen trafficdiff_16_15 = itsscount16_per100 - itsscount15_per100

*-------------------------------------------------------------------------------
* correlations reported in the text
*-------------------------------------------------------------------------------

cor trafficdiff_16_15 peddiff_16_15
cor trafficdiff_15_14 peddiff_15_14


*-------------------------------------------------------------------------------
* graph: 2015 to 2016
*-------------------------------------------------------------------------------

tempfile points
binsreg trafficdiff_16_15 peddiff_16_15, savedata(`points')
append using `points'

#delimit;
twoway (scatter trafficdiff_16_15 peddiff_16_15 if trafficdiff_16_15 < 50,  
	msize(small) msymbol(dot) mcolor(gs11%60) mlwidth(thin))
	(scatter dots_fit dots_x,  
	msize(small) msymbol(dot) mcolor(black) mlwidth(thin)), 
yti("Change in Traffic Stops (2015 to 2016)", size(medlarge)) 
xti("Change in Pedestrian Stops (2015 to 2016)", size(medlarge)) 
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
ylab(-15(5)25, labsize(medlarge) glcolor(white) angle(horizontal))
xlab(-60(10)30, labsize(medlarge))
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
yscale(lcolor(none))
xscale(lcolor(none))
legend(order(1 "Raw data" 2 "Binned data") ring(0) pos(7));
graph export "figures/Figure2b.pdf", replace;

#delimit cr


*-------------------------------------------------------------------------------
* graph: 2014 to 2015
*-------------------------------------------------------------------------------

drop dots*
tempfile points
binsreg trafficdiff_15_14 peddiff_15_14, savedata(`points')
append using `points'


#delimit;
twoway (scatter trafficdiff_15_14 peddiff_15_14,  
	msize(small) msymbol(dot) mcolor(gs11%60) mlwidth(thin))
	(scatter dots_fit dots_x,  
	msize(small) msymbol(dot) mcolor(black) mlwidth(thin)), 
yti("Change in Traffic Stops (2014 to 2015)", size(medlarge)) 
xti("Change in Pedestrian Stops (2014 to 2015)", size(medlarge)) 
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
ylab(-15(5)25, labsize(medlarge) glcolor(white) angle(horizontal))
xlab(-60(10)30, labsize(medlarge))
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
yscale(lcolor(none))
xscale(lcolor(none))
legend(order(1 "Raw data" 2 "Binned data") ring(0) pos(7));
graph export "figures/Figure2a.pdf", replace;

#delimit cr

* End




